<?php

namespace app\common\service\ai;

use think\facade\Db;
use app\common\service\CommonService;

/**
 * AI音频
 */
class AiVoiceService extends CommonService {

    /**
     * 表名
     * @static string
     */
    protected static $table = 'ai_voice';

    /**
     * 列表
     * @param array $data 数据
     * @return array
     */
    public static function getList($data) {
        $list = self::query(self::$table)
                ->leftJoin('order o', 'o.voice_id = a.id')
                ->_equal('a.member_id#member_id')
				->where(['a.type' => 1])
                ->order('a.id desc')
                ->field('a.*,o.order_no')
                ->_page();
        return $list;
    }

    /**
     * 列表
     * @param array $data 数据
     * @return array
     */
    public static function getListByHome($data) {
        $list = self::query(self::$table)
                ->leftJoin('order o', 'o.voice_id = a.id')
                ->_equal('a.member_id#member_id')
                ->order('a.id desc')
                ->field('a.*,o.order_no')
                ->_page(true);
        return $list;
    }

    /**
     * 生成
     * @param array $data 数据
     * @return mixed
     */
    public static function build($data) {
        Db::startTrans();
        // AI音频
        $data['type'] = 1;
        if (!isset($data['speaker_id']) || !$data['speaker_id']) {
            Db::rollback();
            exception("请选择音色");
        }
//        $data['create_time'] = time();
//        $data['update_time'] = time();
        $voice_id = self::model()->_insert(self::$table, $data, false);
        if (!$voice_id) {
            Db::rollback();
            exception();
        }
        // 订单
        $point = 0;
        $byte_rate = SystemConfigService::getDetail('BYTE_RATE')['value'] ?? 0;
        if (isset($data['text'])) {
            $byte = strlen($data['text']);
            $point = $byte * $byte_rate;
        }
        $member_point = self::query('member')->where(['member_id' => $data['member_id']])->_value('remain_point');
        $member_point < $point && exception('会员点数不够');
        $order_no = OrderService::createOrerNo();
        $order_data = [
            'order_no' => $order_no,
            'status' => 1,
            'member_id' => $data['member_id'],
            'voice_id' => $voice_id,
            'ai_type' => 1,
            'type' => 2,
            'scene_type' => 1,
            'point' => $point,
            'before_point' => $member_point,
            'after_point' => $member_point - $point,
            'remark' => OrderService::getRemark(2, 2),
        ];
        $order_id = self::model()->_insert('order', $order_data);
        if (!$order_id) {
            Db::rollback();
            exception();
        }
        // 用户扣点
        $result = self::model()->_update('member', ['remain_point' => Db::raw("remain_point-{$point}")], ['member_id' => $data['member_id']]);
        if (!$result) {
            Db::rollback();
            exception();
        }
        Db::commit();
        return success();
    }

    /**
     * 删除
     * @param array $data 数据
     * @return mixed
     */
    public static function del($data) {
        $result = self::model()->_delete(self::$table, self::ID($data));
        !$result && exception();
        return success();
    }

    /**
     * 列表
     * @param array $data 数据
     * @return array
     */
    public static function getListBySelect($data) {
        $list = self::query(self::$table)->_equal('member_id')->order('id desc')->_select();
        return $list;
    }
	
	/**
     * 点数
     * @param array $data 数据
     * @return mixed
     */
    public static function getPoint($data) {
		$point = 0;
        $byte_rate = SystemConfigService::getDetail('BYTE_RATE')['value'] ?? 0;
        if (isset($data['text'])) {
            $byte = mb_strlen($data['text'], 'utf8');
            $point = $byte * $byte_rate;
        }
		return $point;
	}

    /**
     * 待生成列表
     * @return array
     */
    public static function getTobeList() {
        $list = self::query(self::$table)
            ->leftJoin('order o', 'o.video_id = a.id')
            ->where(['a.status' => 0])
            ->order('a.id desc')
            ->field('a.*,o.order_no')
            ->_page();
        return $list;
    }

    public static function update(array $array, array $array1)
    {
        self::model()->_update(self::$table, $array1, $array);
    }
}
